package com.zzyl.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import java.time.Duration;
import java.util.Random;

/**
 * @Description SpringCacheConfig
 * @Author songyu
 * @Date 2025-09-26  10:07
 */
@Configuration
public class SpringCacheConfig {

    //统一设置springCache过期时间
    @Bean
    public RedisCacheManager cacheManagerOneDay(RedisConnectionFactory connectionFactory) {
        //生成随机数
        int randomNum = new Random().nextInt(6000);
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                 // 过期时间为基础时间加随机数
                .entryTtl(Duration.ofSeconds(24 * 60 * 60L + randomNum));
                //.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(JACKSON_SERIALIZER));

        return RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(config)
                .transactionAware()
                .build();
    }
}